﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KnitwearCompany.Managers;
using KnitwearCompany.Models;

namespace KnitwearCompany.Controllers
{
    public class AdminController : Controller
    {

        Manager manager = new Manager();

        public ActionResult Admin()
        {
            return View();
        }

        // categories

        public ActionResult Categories()
        {
            return View(manager.GetCategories());
        }

        public ActionResult CreateCategory()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateCategory([Bind(Exclude = "ID")] Category category)
        {
            try
            {
                if (manager.AddCategory(category))
                    return RedirectToAction("Categories");
                else
                    return View("CreateCategory");
            }
            catch
            {
                return View("CreateCategory");
            }
        }

        public ActionResult EditCategory(int id)
        {
            try
            {
                Category category = manager.GetCategory(id);
                if (category != null)
                    return View(category);
                else
                    return View("Categories");
            }
            catch
            {
                return View("Categories");
            }
        }

        [HttpPost]
        public ActionResult EditCategory(Category category)
        {
            try
            {
                if (manager.UpdateCategory(category))
                    return RedirectToAction("Categories");
                else
                    return View("EditCategory");
            }
            catch
            {
                return View("EditCategory");
            }
        }

        public ActionResult DeleteCategory(int id)
        {
            manager.DeleteCategory(id);
            return RedirectToAction("Categories");
        }

        [HttpPost]
        public ActionResult DeleteCategory(int id, FormCollection collection)
        {
            try
            {
                return RedirectToAction("Categories");
            }
            catch
            {
                return View();
            }
        }

        // products

        public ActionResult Products()
        {
            return View(manager.GetProducts());
        }

        public ActionResult CreateProduct()
        {
            ViewData["categories"] = manager.GetCategories()
                .Select(c => new SelectListItem
                {
                    Value = c.Id.ToString(),
                    Text = c.Name
                }); ;
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateProduct([Bind(Exclude = "ID, Category")] Product product, string categoryId)
        {
            try
            {
                product.Category = manager.GetCategory(Convert.ToInt32(categoryId));
                if (manager.AddProduct(product))
                    return RedirectToAction("Products");
                else
                    return CreateProduct();
            }
            catch
            {
                return CreateProduct();
            }
        }

        public ActionResult EditProduct(int id)
        {
            try
            {
                Product product = manager.GetProduct(id);
                if (product != null)
                {
                    ViewData["categories"] = manager.GetCategories()
                        .Select(c => new SelectListItem
                        {
                            Value = c.Id.ToString(), 
                            Text = c.Name
                        });;
                    return View(product);
                }
                else
                    return View("Products");
            }
            catch
            {
                return View("Products");
            }
        }

        [HttpPost]
        public ActionResult EditProduct(Product product, string categoryId)
        {
            try
            {
                product.Category = manager.GetCategory(Convert.ToInt32(categoryId));
                if (manager.UpdateProduct(product))
                    return RedirectToAction("Products");
                else
                {
                    ViewData["categories"] = manager.GetCategories()
                    .Select(c => new SelectListItem
                    {
                        Value = c.Id.ToString(),
                        Text = c.Name
                    });;
                    return View("EditProduct");
                }
            }
            catch
            {
                ViewData["categories"] = manager.GetCategories()
                .Select(c => new SelectListItem
                {
                    Value = c.Id.ToString(),
                    Text = c.Name
                }); ; 
                return View("EditProduct");
            }
        }

        public ActionResult DeleteProduct(int id)
        {
            manager.DeleteProduct(id);
            return RedirectToAction("Products");
        }

        [HttpPost]
        public ActionResult DeleteProduct(int id, FormCollection collection)
        {
            try
            {
                return RedirectToAction("Products");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Orders()
        {
            return View(manager.GetOrders());
        }

        public ActionResult DeleteOrder(int id)
        {
            manager.DeleteOrder(id);
            return RedirectToAction("Orders");
        }

        public ActionResult EditOrder(int id)
        {
            var order = manager.GetOrder(id);
            ViewData["statuses"] = manager.GetStatuses()
                .Select(c => new SelectListItem
                {
                    Value = c.Id.ToString(),
                    Text = c.Name
                }); ;
            return View(order); ;
        }

        [HttpPost]
        public ActionResult EditOrder(Order order, string statusId)
        {
            try
            {
                order.Status = manager.GetStatus(Convert.ToInt32(statusId));
                if (manager.UpdateOrderStatus(order))
                    return RedirectToAction("Orders");
                else
                {
                    ViewData["statuses"] = manager.GetStatuses()
                        .Select(c => new SelectListItem
                        {
                            Value = c.Id.ToString(),
                            Text = c.Name
                        }); ;
                    return View("EditOrder");
                }
            }
            catch
            {
                ViewData["statuses"] = manager.GetStatuses()
                    .Select(c => new SelectListItem
                    {
                        Value = c.Id.ToString(),
                        Text = c.Name
                    }); ;
                return View("EditOrder");
            }
        }

    }
}